/**
 * @name Cross-Module Dependencies
 * @kind problem
 * @problem.severity info
 * @id cpp/cross-module-dependencies
 * @description 分析跨模块依赖关系，包括函数调用和头文件依赖
 */

import cpp

from FunctionCall call, Function caller, Function callee, 
     Include include, File callerFile, File calleeFile
where 
  call.getTarget() = callee
  and call.getEnclosingFunction() = caller
  and caller.getFile() = callerFile
  and callee.getFile() = calleeFile
  // 跨模块调用：不同目录下的文件
  and regexpCapture(callerFile.getRelativePath(), "^(.*?)/[^/]+$", 1) != 
      regexpCapture(calleeFile.getRelativePath(), "^(.*?)/[^/]+$", 1)
  // 排除标准库
  and not calleeFile.getRelativePath().matches("%/usr/%")
  and not calleeFile.getRelativePath().matches("%stdlib.h%")
select 
  callerFile.getRelativePath() as callerFile,
  regexpCapture(callerFile.getRelativePath(), "^(.*?)/[^/]+$", 1) as callerModule,
  caller.getName() as callerFunction,
  call.getLocation().getStartLine() as callLine,
  calleeFile.getRelativePath() as calleeFile,
  regexpCapture(calleeFile.getRelativePath(), "^(.*?)/[^/]+$", 1) as calleeModule,
  callee.getName() as calleeFunction,
  callee.getQualifiedName() as calleeQualifiedName,
  "Cross-module call: " + regexpCapture(callerFile.getRelativePath(), "^(.*?)/[^/]+$", 1) + 
    " -> " + regexpCapture(calleeFile.getRelativePath(), "^(.*?)/[^/]+$", 1)

